import { Component, OnInit, Injector, ViewChild, EventEmitter, Output } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';
import {
    GetStock_AuditInputDto,
    Stock_BillListDto,
    StockZT_LostBillItemServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
    templateUrl: './create-or-edit-tooling-lost-opinion-modal.component.html',
    animations: [appModuleAnimation()],
    selector: 'createToolingLostOpinionModalComponent'
})
export class CreateToolingLostOpinionModalComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    @ViewChild('detailDataTable') detailDataTable: Table;
    @ViewChild('detailPaginator') detailPaginator: Paginator;
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() modalUpdate: EventEmitter<any> = new EventEmitter<any>();
    auditDto = new GetStock_AuditInputDto();
    selectedBill: Stock_BillListDto;
    index;
    constructor(injector: Injector, private _billService: StockZT_LostBillItemServiceProxy) {
        super(injector);
    }
    active = false;
    saving = false;
    ngOnInit() { }

    //新增
    show(selectedBill, index): void {
        this.active = true;
        this.selectedBill = selectedBill;
        this.index = index;
        if (this.index == '0') {
            this.auditDto.f_Opinion = '同意';
        }
        if (this.index == '1') {
            this.auditDto.f_Opinion = '不同意';
        }
        this.modal.show();
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
    //保存
    save(): void {
        this.auditDto.id = this.selectedBill.id;
        this.primengTableHelper.showLoadingIndicator();
        if (this.index == '0') {
            this.auditDto.f_Status = '同意';
            this._billService.approveToolingAuditBill(this.auditDto).subscribe(() => {
                this.notify.success(this.l('更新成功'));
                this.modalSave.emit();
                this.close();
                this.primengTableHelper.hideLoadingIndicator();
            });
        }
        if (this.index == '1') {
            this.auditDto.f_Status = '退回';
            this._billService
                .sendBackToolingLostBillBase(this.selectedBill.id)
                .subscribe(() => {
                    this._billService.sendBackToolingAuditBill(this.auditDto).subscribe(() => {
                        this.notify.success(this.l('更新成功'));
                        this.modalSave.emit();
                        this.close();
                        this.primengTableHelper.hideLoadingIndicator();
                    });
                });

        }
    }
}
